package com.liujit.standard.basis.admin.config.interceptor;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.liujit.standard.basis.admin.util.ApiUtil;
import com.liujit.standard.basis.core.domain.result.UserInfo;
import com.liujit.standard.basis.core.util.IdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @description: Sql自动填充
 * @author: liujun
 * @create: 2021-03-26 17:09
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        UserInfo sysUser = ApiUtil.getSysUser();
        this.strictInsertFill(metaObject, "id", Long.class, IdGenerator.getId());
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "createUserId", Long.class, sysUser.getId());
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateUserId", Long.class, sysUser.getId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        UserInfo sysUser = ApiUtil.getSysUser();
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateUserId", Long.class, sysUser.getId());
    }
}
